﻿//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2009 , Jirsoft , Ltd.
//------------------------------------------------------------

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Windows.Forms;

namespace DotNet.RemotingServer
{
    using DotNet.DbUtilities;
    using DotNet.Utilities;

    internal static class Program
    {
        private static void ShowChannelProperties(IChannel channel)
        {
            Console.WriteLine("Name: " + channel.ChannelName);
            Console.WriteLine("Priority: " + channel.ChannelPriority);
            if (channel is HttpServerChannel)
            {
                HttpServerChannel httpChannel = channel as HttpServerChannel;
                Console.WriteLine("Scheme: " + httpChannel.ChannelScheme);
            }
            if (channel is IChannelReceiver)
            {
                IChannelReceiver channelReceiver = channel as IChannelReceiver;

                ChannelDataStore data = (ChannelDataStore)channelReceiver.ChannelData;
                if (data != null)
                {
                    foreach (string uri in data.ChannelUris)
                    {
                        Console.WriteLine("URI: " + uri);
                    }
                }
            }
            Console.WriteLine();
        }

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        private static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // 记录日志
            BaseSystemInfo.RecordLog = true;
            BaseSystemInfo.StartupPath = Application.StartupPath;

            // 读取配置文件
            BaseSystemInfo.ConfigurationFrom = ConfigurationCategory.UserConfig;
            BaseConfiguration.GetSetting();

            // 主应用程序集名
            BaseSystemInfo.MainAssembly = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
            RemotingConfiguration.Configure(ConfigHelper.FileName, false);

            // 这是调试程序，看程序是否运行正常的
            IChannel[] channels = ChannelServices.RegisteredChannels;
            foreach (IChannel channel in channels)
            {
                ShowChannelProperties(channel);
            }
            System.Console.WriteLine("Remoting 服务器端已启动。。。");
            System.Console.WriteLine("服务器 当前时间：" + DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat));
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            System.Console.WriteLine("数据库服务器 当前时间：" + dbHelper.GetDBDateTime());

            Application.Run(new FormRemoteServer());
        }
    }
}